<template>
  <div>
    <div style="margin-bottom: 10px">
      <el-card>
        <el-input v-model="params.name" style="width: 200px" placeholder="请输入操作内容"></el-input>
        <el-input v-model="params.username" style="width: 200px; margin-left: 5px" placeholder="请输入操作人"></el-input>
        <el-button type="warning" style="margin-left: 10px" @click="findBySearch()">查询</el-button>
        <el-button type="warning" style="margin-left: 10px" @click="reset()">清空</el-button>
      </el-card>
    </div>

    <div>
        <el-card>
          <el-button size="small" type="danger" @click="delBatch()">批量删除</el-button>
        </el-card>
    </div>

    <div style="margin-top: 10px">
      <el-card>
        <div>
          <el-table :data="tableData" style="width: 100%" ref="table" @selection-change="handleSelectionChange" :row-key="getRowKeys">
            <el-table-column ref="table" type="selection" width="55" align="center" :reserve-selection="true"></el-table-column>
            <el-table-column prop="id" label="序号"></el-table-column>
            <el-table-column prop="name" label="操作内容"></el-table-column>
            <el-table-column prop="time" label="操作时间"></el-table-column>
            <el-table-column prop="username" label="操作人"></el-table-column>
            <el-table-column prop="ip" label="ip"></el-table-column>
            <el-table-column label="操作"  align="center">
              <template slot-scope="scope">
                <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
                  <el-button slot="reference" type="danger" style="margin-left: 5px">删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div style="margin-top: 10px">
          <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="params.pageNum"
              :page-sizes="[5, 10, 15, 20]"
              :page-size="params.pageSize"
              layout="total, sizes, prev, pager, next"
              :total="total">
          </el-pagination>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  data() {
    return {
      params: {
        name: '',
        username: '',
        pageNum: 1,
        pageSize: 5
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form: {}
    }
  },
  // 页面加载的时候，做一些事情，在created里面
  created() {
    this.findBySearch();
  },
  // 定义一些页面上控件出发的事件调用的方法
  methods: {
    findBySearch() {
      request.get("/log/search", {
        params: this.params
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.list;
          this.total = res.data.total;
        } else {
          this.$message({
            message: res.msg,
            type: 'success'
          });
        }
      })
    },
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 5,
        name: '',
        username: '',
      }
      this.findBySearch();
    },
    handleSizeChange(pageSize) {
      this.params.pageSize = pageSize;
      this.findBySearch();
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum;
      this.findBySearch();
    },
    submit() {
      request.post("/log", this.form).then(res => {
        if (res.code === '0') {
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.findBySearch();
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    del(id) {
      request.delete("/log/" + id).then(res => {
        if (res.code === '0') {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.findBySearch();
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    delBatch() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请勾选您要删除的项")
        return
      }
      this.$confirm('确定要删除该条记录吗?', '确认删除', {confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'}).then(() => {
        request.put("/log/delBatch", this.multipleSelection).then(res => {
          if (res.code === '200') {
            this.$message.success("批量删除成功")
            this.Search()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => {
        // 取消删除操作
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    getRowKeys(row) {
      return row.id;
    }
  }
}
</script>
